package com.vn.gofriends.activity;

/**
 * Created by jacobTran-7437 on 2/24/2015.
 */

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.vn.gofriends.R;

public class LoginSignupActivity extends Activity {
	public static final String TAG = "LoginSignupActivity";
	// Declare Variables
	Button loginbutton;
	TextView signup;
	String usernametxt;
	String passwordtxt;
	EditText password;
	EditText username;

	// LoginButton loginFaceBook;
	// private UiLifecycleHelper uiHelper;

	private static final List<String> PERMISSIONS = Arrays
			.asList("publish_actions");
	ProgressDialog progressDialog;

	/**
	 * Called when the activity is first created.
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// uiHelper = new UiLifecycleHelper(this, statusCallback);
		// uiHelper.onCreate(savedInstanceState);
		// Get the view from main.xml
		setContentView(R.layout.loginsignup);
		// Locate EditTexts in main.xml
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);

		// Locate Buttons in main.xml
		loginbutton = (Button) findViewById(R.id.login);
		signup = (TextView) findViewById(R.id.signup);
		// loginFaceBook = (LoginButton) findViewById(R.id.fb_login_button);
		// Login Button Click Listener
		loginbutton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// Retrieve the text entered from the EditText
				usernametxt = username.getText().toString();
				passwordtxt = password.getText().toString();
				// Validate the log in data
				boolean validationError = false;
				StringBuilder validationErrorMessage = new StringBuilder(
						getResources().getString(R.string.error_intro));
				if (isEmpty(username)) {
					validationError = true;
					validationErrorMessage.append(getResources().getString(
							R.string.error_blank_username));
				}
				if (isEmpty(password)) {
					if (validationError) {
						validationErrorMessage.append(getResources().getString(
								R.string.error_join));
					}
					validationError = true;
					validationErrorMessage.append(getResources().getString(
							R.string.error_blank_password));
				}
				validationErrorMessage.append(getResources().getString(
						R.string.error_end));

				// If there is a validation error, display the error
				if (validationError) {
					Toast.makeText(LoginSignupActivity.this,
							validationErrorMessage.toString(),
							Toast.LENGTH_LONG).show();
					return;
				}
				// Set up a progress dialog
				final ProgressDialog dlg = new ProgressDialog(
						LoginSignupActivity.this);
				dlg.setTitle("Please wait.");
				dlg.setMessage("Logging in.  Please wait...");
				dlg.show();
				// Send data to Parse.com for verification
				ParseUser.logInInBackground(usernametxt.trim(),
						passwordtxt.trim(), new LogInCallback() {
							public void done(ParseUser user, ParseException e) {
								if (user != null) {
									// If user exist and authenticated, send
									// user to Welcome.class
									Intent intent = new Intent(
											LoginSignupActivity.this,
											Welcome.class);
									startActivity(intent);
									dlg.cancel();
									finish();
								} else {
									Toast.makeText(
											getApplicationContext(),
											"No such user exist, please signup",
											Toast.LENGTH_LONG).show();
									dlg.cancel();
								}
							}
						});
			}
		});
		// Sign up Button Click Listener
		signup.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				startActivity(new Intent(getApplicationContext(),
						SignupActivity.class));
			}
		});
		// login facebook
		// loginFaceBook.setUserInfoChangedCallback(new
		// LoginButton.UserInfoChangedCallback() {
		// @Override
		// public void onUserInfoFetched(GraphUser graphUser) {
		// if (graphUser != null) {
		// // Save new user data into Parse.com Data Storage
		//
		// Intent intent = new Intent(
		// LoginSignupActivity.this,
		// Welcome.class);
		// startActivity(intent);
		// finish();
		//
		// }
		// }
		// });

	}

	private boolean isEmpty(EditText etText) {
		if (etText.getText().toString().trim().length() > 0) {
			return false;
		} else {
			return true;
		}
	}

	// private Session.StatusCallback statusCallback = new
	// Session.StatusCallback() {
	// @Override
	// public void call(Session session, SessionState state,
	// Exception exception) {
	// if (state.isOpened()) {
	// Log.d("FacebookSampleActivity", "Facebook session opened");
	// } else if (state.isClosed()) {
	// Log.d("FacebookSampleActivity", "Facebook session closed");
	// }
	// }
	// };

	@Override
	public void onResume() {
		super.onResume();
		// uiHelper.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		// uiHelper.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// uiHelper.onDestroy();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// uiHelper.onActivityResult(requestCode, resultCode, data);
		// ParseFacebookUtils.finishAuthentication(requestCode, resultCode,
		// data);
	}

	@Override
	public void onSaveInstanceState(Bundle savedState) {
		super.onSaveInstanceState(savedState);
		// uiHelper.onSaveInstanceState(savedState);
	}

}